package com.zretc.support.controller;

import com.zretc.support.entity.*;
import com.zretc.support.service.CourseService;
import com.zretc.support.service.ExamPaperService;
import com.zretc.support.service.TopicmultService;
import com.zretc.support.service.TopicsingleService;
import com.zretc.support.tool.ApiResultHandler;
import com.zretc.support.tool.PageData;
import io.swagger.models.auth.In;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api")
public class ExampaperController {

    @Autowired
    ExamPaperService examPaperService;
    @Autowired
    CourseService courseService;
    @Autowired
    TopicmultService topicmultService;
    @Autowired
    TopicsingleService topicsingleService;

    Logger logger = LoggerFactory.getLogger(ExampaperController.class);

    @RequestMapping(value = "exambypage",method = RequestMethod.GET)
    public PageData getAllExamPaperByPage(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                                          @RequestParam(value = "select",defaultValue = "") int select,
                                          @RequestParam(value = "page",defaultValue = "") int page,
                                          @RequestParam(value = "size",defaultValue = "") int size){
        // 2.根据教师名 -- 模糊查询教师编号 -- 再查询课程编号

        // 1.根据课程名 模糊查询到 课程编号
        List<Long> courseId = courseService.getCourseByCourseName(keyword);
        PageData pageData = new PageData();
        Page pages = examPaperService.getExampaperByPage(courseId,page,size);
        List<Exampaper> exampapers = pages.getContent();
        for (int i = 0; i < exampapers.size(); i++) {
            exampapers.get(i).setCount(examPaperService.countExampaperBypaperId(exampapers.get(i).getPaperId()));
        }
        pageData.setRecords(exampapers); // 数据
        pageData.setCurrent(page); // 当前页
        pageData.setSize(size); // 每页数量
        pageData.setTotal((int)pages.getTotalElements()); // 总记录数
        return pageData;
    }

    @RequestMapping(value = "getalltopicsingle",method = RequestMethod.GET)
    public List<Topicsingle> getAllTopicsSingle(){

        return topicsingleService.getAllTopicsingle();
    }

    @RequestMapping(value = "getalltopicmult",method = RequestMethod.GET)
    public List<Topicmult> getAllTopicsMult(){
        return topicmultService.getAllTopic();
    }

    @RequestMapping(value = "generatepaper",method = RequestMethod.POST)
    public int generateExamPaper(@RequestBody Exampaper exampaper){
        logger.info("exampaper:"+exampaper);
        logger.info("course:"+exampaper.getCourse().getCourseId());
        exampaper = examPaperService.generatePaper(exampaper);
        if (exampaper != null){
            return 200;
        }
        return 0;
    }

    @RequestMapping(value = "randgenerate",method = RequestMethod.GET)
    public int randGenerateExamPaper(Integer courseId,Integer num1,Integer num2,Integer num3,Integer num4,Integer num5){
        logger.info("cid:"+courseId);
        logger.info("num1:"+num1);
        examPaperService.randomGeneratePaper((long)courseId, num1, num2, num3, num4, num5);
        return 200;
    }

    @RequestMapping(value = "importexcel",method = RequestMethod.POST)
    public int uploadExcel(MultipartFile file,Integer courseId){
        logger.info("cid:"+courseId);
        logger.info("file:"+file.getSize());
        int n = examPaperService.excelGenerate((long)courseId,file);
        if (n == 1){
            return 200;
        }
        return 0;
    }

    @RequestMapping(value = "getpaperbyid/{paperId}",method = RequestMethod.GET)
    public Exampaper getExampaperByPaperId(@PathVariable("paperId") Integer paperId){
        Exampaper exampaper = examPaperService.getExampaperByPaperId(paperId);
        exampaper.setCount(examPaperService.countExampaperBypaperId(paperId));
        return exampaper;
    }

    @RequestMapping(value = "getpapertopicmult/{paperId}",method = RequestMethod.GET)
    public List<Topicmult> getPaperTopicmult(@PathVariable("paperId") Integer paperId){
        Exampaper exampaper = examPaperService.getExampaperByPaperId(paperId);
        String s = exampaper.getPaperchoose();
        s = s.substring(0,s.length()-1);
        String[] ss = s.split(",");
        List<Topicmult> topicmultList = new ArrayList<>();
        for (int i = 0; i < ss.length; i++) {
            topicmultList.add(topicmultService.getTopicmultById(ss[i]));
        }
        return topicmultList;
    }

    @RequestMapping(value = "getpapertopicsingle/{paperId}",method = RequestMethod.GET)
    public List<Topicsingle> getPaperTopicsingle(@PathVariable("paperId") Integer paperId){
        Exampaper exampaper = examPaperService.getExampaperByPaperId(paperId);
        String s = exampaper.getPapertopics();
        s = s.substring(0,s.length()-1);
        String[] ss = s.split(",");
        List<Topicsingle> topicsingleList = new ArrayList<>();
        for (int i = 0; i < ss.length; i++) {
            topicsingleList.add(topicsingleService.getTopicsingleByTopicId(ss[i]));
        }
        return topicsingleList;
    }

    /**
     * 根据试卷编号查询题目
     *
     * @param a
     * @return
     */
    @RequestMapping(value = "/EP")
    public ApiResult getPage(
            @RequestParam("keywords") Integer a) {
        //获得题目编号
        String topic = examPaperService.getExampaperByPaperId(a).getPapertopics();
        String[] b = topic.split(",");
        List<Topicsingle> map = new ArrayList<>();
        //通过遍历，添加题目信息到list列表
        for (int i = 0; i < b.length; i++) {
            if(topicsingleService.getTopicsingleByTopicId(b[i]).getTopicType() == 3) {
                map.add(topicsingleService.getTopicsingleByTopicId(b[i]));
            }
        }

        return ApiResultHandler.buildApiResult(200,"考试编号不存在",map);
    }

}
